package graduate.cslg.com.universityshopping.bbs;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import java.text.SimpleDateFormat;
import java.util.Date;

import graduate.cslg.com.universityshopping.R;
import graduate.cslg.com.universityshopping.net.LoginResponse;
import graduate.cslg.com.universityshopping.net.RetrofitDevelop;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class BBSEditActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView backText;
    private EditText titleEdit;
    private EditText contentEdit;
    private Button postButton;
    private Toolbar mToolbar;
    private Spinner typeSpinner;
    private String[] typeName = {"二手市场", "学术交流", "日常生活", "其他"};
    private String getTypeName;
    private String getUserName;
    private String getUserAccount;
    private LoginResponse mLoginResponse;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bbsedit);

        initViews();
        setSupportActionBar(mToolbar);

        SharedPreferences preferences = this.getSharedPreferences("user_info",MODE_PRIVATE);
        getUserAccount = preferences.getString("user_account", "Z09315222");
        getUserName = preferences.getString("user_name", "Tom Wang");

    }

    private void initViews() {
        backText = findViewById(R.id.text_back);
        titleEdit = findViewById(R.id.bbs_title);
        contentEdit = findViewById(R.id.bbs_content);
        postButton = findViewById(R.id.bbs_post);
        mToolbar = findViewById(R.id.toolbar);
        typeSpinner = findViewById(R.id.bbs_type);
        postButton.setOnClickListener(this);
        backText.setOnClickListener(this);

        ArrayAdapter<String> type_adapter = new ArrayAdapter<>(this,
                android.R.layout.simple_spinner_dropdown_item,typeName);
        typeSpinner.setAdapter(type_adapter);
        typeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                getTypeName = typeName[position];
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bbs_post:
                String title = titleEdit.getText().toString();
                String content = contentEdit.getText().toString();
                if (title.length() == 0 || content.length() == 0) {
                    Toast.makeText(this, "请检查输入！", Toast.LENGTH_SHORT).show();
                } else {
                    bbsPost(title, content);
                }
                break;
            case R.id.text_back:
                finish();
                break;
        }
    }

    private void bbsPost(String title, String content) {
        RetrofitDevelop.getTestService()
                .topicPost(getUserAccount, getUserName, title, content, getTypeName, getNowTime())
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<LoginResponse>() {
                    @Override
                    public void onCompleted() {
                        if (mLoginResponse.getStatus().equals("true")) {
                            Intent intent = new Intent();
                            setResult(RESULT_OK, intent);
                            finish();
                        } else {
                            Toast.makeText(BBSEditActivity.this, "发布失败，请检查输入是否正确", Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(LoginResponse loginResponse) {
                        mLoginResponse = loginResponse;
                    }
                });
    }

    public static String getNowTime(){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(System.currentTimeMillis());
        return simpleDateFormat.format(date);
    }
}
